<?php
/**
 * STEP 2
 */

// LC_ALL=en_US.UTF-8 /usr/bin/casperjs capture.js '{$url}'
include_once('../lib/config.php'); 
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/function.php');

$redis = new Redis();  
$ret = $redis->connect("localhost","6379");  //php客户端设置的ip及端口
if(!$ret) die('redis connction faild');
$redis->auth(REDIS_PASS);
$redis->select(10);

$info_new_count = 0;
while (true) {
    $stop = $redis->get('QISUU_QUEUE_STOP');
    if($stop) {
        echo2('stop', 'QISUU_INFO'); break;
    }

    $url = $redis->lPop('QISUU_INFO_LINK');
    if($url) $preUrl = $url;

    if(!$url) {
        //if($preUrl) $redis->lPush('QISUU_INFO_LINK', $preUrl);
        echo2('empty', 'QISUU_INFO'); break;
    }

    $filename  = QISUU_NEW . 'htmls/' . urlencode($url);
    if(file_exists($filename) && filesize($filename)>5000) {
        //echo2("$filename file_exists");
        continue;
    }
    //--proxy=127.0.0.1:1080 --proxy-type=socks5
    $shell = "LC_ALL=en_US.UTF-8 /usr/bin/casperjs qisuu.js '{$url}' '".QISUU_NEW."htmls/'";
    echo2($shell, 'QISUU_INFO');
    $result = shell_exec($shell);
    echo2($result, 'QISUU_INFO');
    $info_new_count++;
}

$redis->lPush('stats_qisuu_info', "[".date('Y-m-d H:i:s')."], htmls_new:{$info_new_count}");
echo2('新增详情页htmls源文件：'.$info_new_count, 'QISUU_INFO');

//shell_exec("mkdir -p " . QISUU_NEW . 'data/');
//提取详情页信息
$old = $new = 0;
$files = scandir(QISUU_NEW . 'htmls/');
foreach ($files as $k=>$file) {
    if(!stristr($file, '.html')) {
        continue;
    }

    $filename = QISUU_NEW . 'data/' . $file;
    if(file_exists($filename)) {
        $old++;
        continue;
    }

    $data = [];
    $contents = file_get_contents(QISUU_NEW . 'htmls/'.$file);
    
    //提取小说类型
    $item = cut('<div class="wrap position">', '</div>', $contents);
    preg_match_all('/<a[^>]*>(.+?)<\/a>/is', $item, $matches);

    $data['classname'] = $matches[1][2];
    $data['typename']  = $data['classname'];
    $data['title']     = $matches[1][3];

    if(!$data['classname'] || !$data['typename'] || !$data['title']) {
        echo2($k . ' '. $file . ' 提取信息不全1,'.json_encode($data), 'QISUU_INFO');
        unlink(QISUU_NEW . 'htmls/'.$file);
        continue;
    }

    //小说信息
    preg_match_all('/<li class="small">(.+?)<\/li>/i', $contents, $matches);
    foreach ($matches[1] as $v) {
        $v = explode('：', $v);
        $data['info'][] = trim(strip_tags($v[1]));
    }

    //小说封面
    $item = cut('<div class="detail_pic"><img src="', '"', $contents);
    if(!stristr($item, 'http')) {
        $item = "https://www.qisuu.com" . $item;
    }
    $data['thumb'] = $item;

    //简介
    $item = cut('<div class="showInfo">', '<font', $contents);
    if(stristr($item, '<font')) {
        $item = preg_replace('/<font size=2[^>]*>(.*?)<\/font>/i', '', $item);
        $item = preg_replace('/<font[^>]*>(.*?)<\/font>/i', '', $item);
    }
    $item = preg_replace('/\s+/', '', $item);
    $item = preg_replace('/(<\/div>){2,}/i', '</div>', $item);
    $item = str_ireplace('<div>', '<p>', $item);
    $item = str_ireplace('</div>', '</p>', $item);
    $data['description'] = $item;

    //下载地址
    $item = cut('<div class="showDown">', '</ul>', $contents);
    $item = cut('get_down_url(', ')', $item);
    $item = explode(',', $item);
    $data['download'] = str_replace("'", "", $item[1]);
    //preg_match_all('/href=\'(.+?)\'/i', $item, $matches);
    //$data['download'] = $matches[1];

    if(!$data['download'] || !$data['description']) {
        echo2($k . ' '. $file . ' 提取信息不全2,'.json_encode($data), 'QISUU_INFO');
        unlink(QISUU_NEW . 'htmls/'.$file);
        continue;
    }

    //保存数据
    file_put_contents($filename, json_encode($data));

    echo2($k . ' '. $filename . ' saved', 'QISUU_INFO');
    $new++;
}

$redis->lPush('stats_qisuu_info', "[".date('Y-m-d H:i:s')."], data_old:$old, data_new:$new");
echo2("old:$old, new:$new", 'QISUU_INFO');

sleep(10);

echo2('sleep exit', 'QISUU_INFO');